package com.itheima.health.util;

import org.mindrot.jbcrypt.BCrypt;
import org.springframework.util.DigestUtils;

/**
 * @author tian
 * @TIME 2023/1/6 11:56 上午
 * @description 密码验证工具类：1不加密 2md5加密 3bcrypt加密
 */
public class CheckPasswordUtils {
    public static boolean checkPassword(String password, String dbPassword) {
        if (dbPassword.startsWith("{md5}")) {
            //如果是md5密码
            String mdPassword = DigestUtils.md5DigestAsHex(password.getBytes());
            return mdPassword.equals(dbPassword.replace("{md5}", ""));
        } else if (dbPassword.startsWith("{bcrypt}")) {
            //如果是bcrypt密码
            return BCrypt.checkpw(password, dbPassword.replace("{bcrypt}", ""));
        }
        //未加密
        return password.equals(dbPassword);
    }
}
